#include "TableModel.h"

TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)
{
	list=new MusicList();
	connect(list, SIGNAL(dataChange(int)), this, SLOT(onListDataChanged(int)));
	connect(list, SIGNAL(dataAdded()), this, SLOT(onListDataAdded()));
}

TableModel::TableModel(MusicList* list, QObject *parent) : QAbstractTableModel(parent)
{
	this->list=list;
}

int TableModel::rowCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);
	return list->size();
}

int TableModel::columnCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);
	
	return Track::columns.size();
}


QVariant TableModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())
	{
    		return QVariant();
  	}
  	else if(index.row()>=list->size()||index.row()<0)
  	{
   		return QVariant();
  	}
  	else if(role==Qt::DisplayRole)
  	{
    		return (*(*list)[index.row()])[index.column()];
  	}
  	return QVariant();
}

MusicList* TableModel::getList()
{
	return list;
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (role!=Qt::DisplayRole)
	{
		return QVariant();
	}

	if (orientation==Qt::Horizontal) 
	{
		if(section<0||section>=Track::columns.size())
		{
			return QVariant();
		}
		Track::fillColumns();
		return Track::columns[section];
		
	}
		
	return QVariant();
}

bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{
	Q_UNUSED(index);
	beginInsertRows(QModelIndex(), position, position+rows-1);
	/*for (int row=0; row < rows; row++) 
	{
	 	Track* track=new Track();
	 	list->add(track);
	}*/	
	endInsertRows();
	return true;
}

 bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
 {
     Q_UNUSED(index);
     beginRemoveRows(QModelIndex(), position, position+rows-1);
     for (int row=0; row < rows; ++row) 
     {
     	list->remove(position);
     }
     endRemoveRows();
     return true;
 }


bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if (index.isValid() && role == Qt::EditRole) 
	{
 		int row = index.row();
		Track* track=(*list)[row];
 		switch(index.column())
 		{
 			case 0:
 			track->setTrackNumber(value.toInt());
 			break;
 			case 1:
 			track->setTitle(value.toString());
 			break;
 			case 2:
 			track->setArtist(value.toString());
 			break;
 			case 3:
 			track->setAlbum(value.toString());
 			break;
 			case 4:
 			track->setGenre(value.toString());
 			break;
 			case 5: 
 			track->setLength(value.toInt());
 			break;
 			case 6:
 			track->setPath(value.toString());
 			break;
 			default:
 			return false;
 		}
 		emit(dataChanged(index, index));

 		return true;
	}

	return false;
}

Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
	if (!index.isValid())
	{
		return Qt::ItemIsEnabled;
	}
	return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

void TableModel::onListDataChanged(int row)
{
	qDebug() << "row:" << row; 
	QModelIndex indexLeft=createIndex(row, 0, 0);
	QModelIndex indexRight=createIndex(row, Track::columns.size()-1, 0);
	emit(dataChanged(indexLeft, indexRight));
}

void TableModel::onListDataAdded()
{
	int i;
	qDebug() << "Inserting row at position" << list->size()-1;
	insertRows(list->size()-1, 1, QModelIndex());
	QModelIndex indexLeft=createIndex(list->size()-1, 0, 0);
	QModelIndex indexRight=createIndex(list->size()-1, Track::columns.size()-1, 0);
	emit(dataChanged(indexLeft, indexRight));
}

QModelIndex TableModel::giveIndex(int row, int column)
{
	return createIndex(row, column, 0);
}





